
package com.cory;

import java.util.Arrays;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import com.beust.jcommander.internal.Lists;
import com.cory.admin.sys.config.AdminConfig;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.boot.BaseApplication;
import com.cory.boot.config.FreemarkerConfig;
import com.cory.boot.utils.SpringUtils;

public class AdminApplication extends BaseApplication {
    /**
     * 监听事件
     */
    private static List<ApplicationListener<?>> listeners = Lists.newArrayList();
	
	public static ConfigurableApplicationContext run(String[] args) {
	    SpringApplication application = new SpringApplication(AdminApplication.class);
	    
//	    listeners.add(new NettyListener());
//	    ApplicationListener<?>[] listeners = new ApplicationListener[AdminApplication.listeners.size()];
//	    application.addListeners(AdminApplication.listeners.toArray(listeners));
	    ConfigurableApplicationContext content = application.run(args);
	    
	    AdminConstants.config = content.getBean(AdminConfig.class);
	    AdminConstants.MODEL = AdminConstants.config.getModel();
	    AdminConstants.REDIS_PREFIX_TOKEN = AdminConstants.config.getAppId() + ":TOKEN:";
	    AdminConstants.COOKIE_TOKEN = AdminConstants.config.getAppId() + "_token";
	    
	    FreemarkerConfig freemarkerConfig = SpringUtils.getBean(FreemarkerConfig.class);
	    freemarkerConfig.setStaticModels("AdminConstants", "com.cory.admin.sys.config.AdminConstants");
	    freemarkerConfig.setStaticModels("AdminUtils", "com.cory.admin.sys.utils.AdminUtils");
	    return content;
	}
	
	/**
	 * 添加监听事件
	 * @param application
	 * @author xiongcong 2020-01-21
	 */
	protected static void addListeners(ApplicationListener<?>... listeners) {
	    AdminApplication.listeners.addAll(Arrays.asList(listeners));
	}
}

